#include "led.h"
#include <string.h>

DEV_REG_DEF(led,Led_t)

DEV_RET_t
led_on(Device_t *pstDevice){

    // 输入参数检查
    if(NULL == pstDevice || NULL == pstDevice->pvFeature) return DEV_PARAM_ERR;

    // 设备检测
    CHECK_DEVICE(pstDevice);

    // 转换为Led_t结构体
    Led_t *pstLed = GET_DEVICE(pstDevice, Led_t);
    
    // 输入参数检查
    if(NULL == pstLed->HW.pfnWrite) {DEV_RETURN(DEV_PARAM_ERR)};

    // 点亮LED
    pstLed->SW.u8State = pstLed->SW.u8Active;
    pstLed->HW.pfnWrite(pstLed->SW.u8State);

    // LED执行成功
    DEV_RETURN(DEV_OK);
}

DEV_RET_t
led_off(Device_t *pstDevice){

    // 输入参数检查
    if(NULL == pstDevice || NULL == pstDevice->pvFeature) return DEV_PARAM_ERR;
    
    // 设备检测
    CHECK_DEVICE(pstDevice);

    // 转换为Led_t结构体
    Led_t *pstLed = GET_DEVICE(pstDevice, Led_t);

    // 输入参数检查
    if(NULL == pstLed->HW.pfnWrite) {DEV_RETURN(DEV_PARAM_ERR)};

    // 熄灭LED
    pstLed->SW.u8State = !pstLed->SW.u8Active;
    pstLed->HW.pfnWrite(pstLed->SW.u8State);

    // LED执行成功
    DEV_RETURN(DEV_OK);
}

DEV_RET_t
led_toggle(Device_t *pstDevice){

    // 输入参数检查
    if(NULL == pstDevice || NULL == pstDevice->pvFeature) return DEV_PARAM_ERR;
    
    // 设备检测
    CHECK_DEVICE(pstDevice);

    // 转换为Led_t结构体
    Led_t *pstLed = GET_DEVICE(pstDevice, Led_t);
        
    // 输入参数检查
    if(NULL == pstLed->HW.pfnWrite) {DEV_RETURN(DEV_PARAM_ERR)}

    // 切换LED状态
    pstLed->SW.u8State = !pstLed->SW.u8State;
    pstLed->HW.pfnWrite(pstLed->SW.u8State);

    // LED执行成功
    DEV_RETURN(DEV_OK);
}










